package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.game.ActorRole;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIPalette;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.ArrayList;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;

public class UIAutoAddFriend extends UIAbstractWindow
{
	private final int LIST_RECT_H = 24; // 列表高 ，通用 、内
	private final int LIST_SPACE = 3; // 标题自间的间隔
	private final int STRING_LIST_Y = (LIST_RECT_H - AbstractGameScreen.FONT_H) >> 1; //文字顶部距离
	private final int LIST_HELGHT = LIST_RECT_H * 5;// 所有列表的高
	private static final int LISTNAME_NAME_WIDTH = 120;
	private static final int LISTNAME_OTHER_WIDTH = 60;
	private final int LISTNAME_NAME_X = UITools.X_BG
			+ ((UITools.W_BG - LISTNAME_NAME_WIDTH - LISTNAME_OTHER_WIDTH - LIST_SPACE) >> 1); // 玩家名字标题X
	private final int LISTNAME_NAME_Y = UITools.Y_BG + UITools.INITY_CONTENT; // 玩家名字标题Ｙ
	private final int LIST_FRAME_X = LISTNAME_NAME_X; // 玩家姓名列表x
	private final int LIST_FRAME_Y = LISTNAME_NAME_Y + LIST_RECT_H + LIST_SPACE;// 玩家姓名列表Y
	private final int MSG_FRAME_H = UITools.H_BG - LIST_HELGHT - LIST_RECT_H - LIST_SPACE * 3 - 50;
	private final int MSG_FRAME_W = LISTNAME_NAME_WIDTH + LISTNAME_OTHER_WIDTH + LIST_SPACE;
	private String tishiMSG = "系统为您自动添加以上好友，方便您摇钱树浇水等功能。"; //显示的提示文字

	public UIAutoAddFriend(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
		splitSting();
	}

	private void splitSting()
	{
		MSGSTRING = Tools.splitString(tishiMSG, AbstractGameScreen.s_font, '|', MSG_FRAME_W - 6);
	}

	/**
	 * 系统自动添加好友名单
	 * @param dis
	 * @throws IOException
	 */
	public static void recvAutoAddFriend(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("系统自动添加好友");
		ArrayList list = new ArrayList();
		byte num = dis.readByte();
		for (int i = 0; i < num; i++) {
			SimpleRole role = new SimpleRole();
			role.m_grade = dis.readInt();
			role.m_name = dis.readUTF();
			list.addElement(role);
		}
		UIAutoAddFriend window = (UIAutoAddFriend) UIManager.getInstance()
				.getWindowById(UIWindowFactory.WINDOW_ID_SOCIAL_AUTOADDFRIEND);
		if (window == null) {
			window = (UIAutoAddFriend) UIManager.getInstance()
					.createWindow(UIWindowFactory.WINDOW_ID_SOCIAL_AUTOADDFRIEND);
		}
		synchronized (window) {
			window.autoAddFriendList = list;
		}
	}

	public ArrayList autoAddFriendList = null;
	public String tittleString = "添加好友";

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, tittleString);
		paintListTitle(g, "玩家姓名", "等级");
		paintRectList(g);
	}

	/**
	 * 列表标题
	 * @param g
	 * @param str1
	 * 2个标题名字
	 * @param str2
	 * @param str3
	 * @param str4
	 */
	private void paintListTitle(Graphics g, String str1, String str2)
	{
		UITools.drawListTitleItem(g, LISTNAME_NAME_X, LISTNAME_NAME_Y,
				LISTNAME_NAME_WIDTH, LIST_RECT_H, str1);
		UITools.drawListTitleItem(g, LISTNAME_NAME_X + LISTNAME_NAME_WIDTH
				+ LIST_SPACE, LISTNAME_NAME_Y, LISTNAME_OTHER_WIDTH,
				LIST_RECT_H, str2);
		MSGFRAME(g);
	}

	/**
	 * 绘制好友成员列表
	 * 
	 * @param g
	 */
	private void paintRectList(Graphics g)
	{
		drawRectListBG(g);
		if (autoAddFriendList == null || autoAddFriendList.size() == 0) {
			return;
		}
		int listsize = autoAddFriendList.size();
		for (int i = 0; i < listsize; i++) {
			SimpleRole memberRole = (SimpleRole) autoAddFriendList.elementAt(i);
			drawRectMSGItem_H(g, Tools.YELLOW, memberRole, i);
		}
	}

	/***
	 * 绘制队员信息列表数据 行
	 * 
	 * @param g
	 * "玩家姓名", "等级", "职业", "种族"
	 * @param x
	 * 宽中间的X
	 * @param y
	 * @param role
	 */
	private void drawRectMSGItem_H(Graphics g, int colour, SimpleRole role, int hint)
	{
		g.setClip(LIST_FRAME_X + 2, LIST_FRAME_Y + 2 + LIST_RECT_H * hint,
				LISTNAME_NAME_WIDTH - 4, LIST_RECT_H);
		UITools.drawColorString(g, role.m_name, LIST_FRAME_X + 4 + (LISTNAME_NAME_WIDTH >> 1), LIST_FRAME_Y
				+ LIST_RECT_H * hint + STRING_LIST_Y, colour, Graphics.TOP | Graphics.HCENTER);
		g.setClip(LIST_FRAME_X + 2 + LIST_SPACE + LISTNAME_NAME_WIDTH, LIST_FRAME_Y + 2 + LIST_RECT_H * hint,
				LISTNAME_OTHER_WIDTH - 4, LIST_RECT_H);
		UITools.drawColorString(g, role.m_grade + "", LIST_FRAME_X + 4 + LIST_SPACE + LISTNAME_NAME_WIDTH
				+ (LISTNAME_OTHER_WIDTH >> 1), LIST_FRAME_Y + LIST_RECT_H
				* hint + STRING_LIST_Y, colour, Graphics.TOP | Graphics.HCENTER);
	}

	/**
	 * 绘制信息列表 一列
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	private void drawRectListBG(Graphics g)
	{
		UITools.drawListFrame(g, LIST_FRAME_X, LIST_FRAME_Y, LISTNAME_NAME_WIDTH, LIST_HELGHT);
		UITools.drawListFrame(g, LIST_FRAME_X + LIST_SPACE + LISTNAME_NAME_WIDTH, LIST_FRAME_Y, LISTNAME_OTHER_WIDTH,
				LIST_HELGHT);
	}

	public String MSGSTRING[];

	/**
	 * 绘制文字框
	 */
	private void MSGFRAME(Graphics g)
	{
		g.setClip(LIST_FRAME_X, LIST_FRAME_Y + LIST_HELGHT + LIST_SPACE, MSG_FRAME_W, MSG_FRAME_H);
		UITools.drawListFrame(g, LIST_FRAME_X, LIST_FRAME_Y + LIST_HELGHT + LIST_SPACE, MSG_FRAME_W, MSG_FRAME_H);
		paintRectStrSocial(g, LIST_FRAME_X + 4, LIST_FRAME_Y + LIST_HELGHT + LIST_SPACE + 5, MSG_FRAME_W, MSG_FRAME_H,
				MSGSTRING);
	}

	/**
	 * 输入带有文字的内容框，其中文字颜色及内框颜色可定制
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param content
	 */
	private static final void paintRectStrSocial(Graphics g, int x, int y, int w, int h, String content[])
	{
		if (content == null || content.equals("")) {
			return;
		}
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		int font_height = AbstractGameScreen.s_font.getHeight() + 1;
		if (content != null && content.length != 0) {
			g.setClip(x, y, w, h);
			for (int i = 0; i < content.length; i++) {
				g.drawString(content[i], x + 4, y + font_height * i + 1, Tools.LEFT_TOP);
			}

		}
	}

	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
				|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			close();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
	}

	public static void textadd()
	{

		Tools.debugPrintln("系统自动添加好友");
		ArrayList list = new ArrayList();
		byte num = 3;
		for (int i = 0; i < num; i++) {
			SimpleRole role = new SimpleRole();
			role.m_grade = 3;
			role.m_name = "古人中";
			list.addElement(role);
		}
		UIAutoAddFriend window = (UIAutoAddFriend) UIManager.getInstance()
				.getWindowById(UIWindowFactory.WINDOW_ID_SOCIAL_AUTOADDFRIEND);
		if (window == null) {
			window = (UIAutoAddFriend) UIManager.getInstance()
					.createWindow(UIWindowFactory.WINDOW_ID_SOCIAL_AUTOADDFRIEND);
		}
		synchronized (window) {
			window.autoAddFriendList = list;
		}

	}

	protected void registerTouch() {
		clearAllRects();
		registerNormalSoft();
		if (autoAddFriendList == null || autoAddFriendList.size() == 0 ) {
			return;
		} else {
			int listsize = autoAddFriendList.size();
			int[][] rect = new int[listsize][];
			for (int i = 0; i < listsize; i++) {
				rect[i] = new int[]{LIST_FRAME_X + 2, LIST_FRAME_Y + 2 + LIST_RECT_H * i,
						LISTNAME_NAME_WIDTH - 4, LIST_RECT_H};
			}
			initTouchRect(rect);
		}
	}

	protected void onTouchRect(int[][] rect, int touchid) {
		GameTouchManager.keyPressedFire();
	}
	
	
}
